package com.t.bricks.bussiness.config.bridge;

import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.t.bricks.bussiness.service.bridge.ProxyServletEx;

/**
 * '请求代理'配置
 * @author huangxinbian
 */
@Configuration
public class ServletProxyConfig {
	
	/**
	 * Cloud注册中心地址(分布式时使用)
	 */
	@Value("${cloud.center.site:}")
	private String cloudCenterUrl;
	
	/**
	 * Cloud注册中心用户名(分布式时使用)
	 */
	@Value("${cloud.center.user:}")
	private String cloudCenterUser;
	
	/**
	 * Cloud注册中心用户密码(分布式时使用)
	 */
	@Value("${cloud.center.password:}")
	private String cloudCenterPass;

	/**
	 * 分布式程序'请求代理'信息,注意:启用此配置则不再访问注册中心查找服务,即cloud.center的配置无效
	 */
	@Value("${cloud.proxy.domains:}")
	private String cloudProxyDomains;
	
    @Bean
    public ServletRegistrationBean<ProxyServletEx> servletServletRegistrationBean() {
    	ProxyServletEx.setCloudCenterUrl(cloudCenterUrl, cloudCenterUser, cloudCenterPass, cloudProxyDomains);
    	
        ServletRegistrationBean<ProxyServletEx> servletRegistrationBean =
        		new ServletRegistrationBean<>(new ProxyServletEx(), "/proxy/*");
        servletRegistrationBean.addInitParameter("targetUri", "null");//随便定义一个,否则会报错
        servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");
        return servletRegistrationBean;
    }
    
}
